<?php

class OutstandingTasksController extends AppController {

    var $name = 'OutstandingTasks';

    function index() {
        $this->OutstandingTask->recursive = 0;
        $this->set('outstandingTasks', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid outstanding task', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('outstandingTask', $this->OutstandingTask->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->OutstandingTask->create();
            if ($this->OutstandingTask->save($this->data)) {
                $this->Session->setFlash(__('The outstanding task has been saved', true));
                $this->redirect(array('controller' => 'clients', 'action' => 'view/' . $this->data['OutstandingTask']['client_id']));
            } else {
                $this->Session->setFlash(__('The outstanding task could not be saved. Please, try again.', true));
                $this->redirect($this->referer());
            }
        }
        $clients = $this->OutstandingTask->Client->find('list');
        $users = $this->OutstandingTask->User->find('list');
        $this->set(compact('clients', 'users'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid outstanding task', true));
            $this->redirect($this->referer());
        }
        if (!empty($this->data)) {
            if ($this->OutstandingTask->save($this->data)) {
                $this->Session->setFlash(__('The outstanding task has been saved', true));
                //$this->redirect(array('controller' => 'clients', 'action' => 'view/' . $this->data['OutstandingTask']['client_id']));
                $this->redirect($this->referer());
            } else {
                $this->Session->setFlash(__('The outstanding task could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->OutstandingTask->read(null, $id);
        }
        $clients = $this->OutstandingTask->Client->find('list');
        $users = $this->OutstandingTask->User->find('list');
        $this->set(compact('clients', 'users'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for outstanding task', true));
            $this->redirect($this->referer());
        }
        if ($this->OutstandingTask->delete($id)) {
            $this->Session->setFlash(__('Outstanding task deleted', true));
            $this->redirect($this->referer());
        }
        $this->Session->setFlash(__('Outstanding task was not deleted', true));
        $this->redirect($this->referer());
    }

    function search($id = null) {
        if ($id == null) {
            echo "No id passed";
        } else {
            $results = $this->OutstandingTask->query("SELECT * FROM  outstanding_tasks where user_id =".$id." and done = 0");
            return $results;
        }
    }

}

?>